﻿#include "af_additem.h"
#include "ui_af_additem.h"

AF_AddItem::AF_AddItem(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::AF_AddItem)
{
    ui->setupUi(this);

    table=TD_ASSEMBLE_FLOW;
    ui->dateEdit->setDate(QDate::currentDate());

    updateTypes();
    updatePartners();
    updateNames();
}

AF_AddItem::~AF_AddItem()
{
    delete ui;
}

/*
void AF_AddItem::setDate(const QString &date)
{
    date_=date;
    if(date_.isEmpty())
        date_=Today;
    month_=date_.section("",0,7);
}
*/

void AF_AddItem::setDate(const QDate &date)
{
    ui->dateEdit->setDate(date);
}

void AF_AddItem::updateTypes()
{
    QStringList items=sp_getCategory("type_",table);
    if(!items.isEmpty())
        ui->types->addItems(items);

    ui->types->addItem(MANUAL_INPUT);
    ui->types->setCurrentIndex(-1);
}

void AF_AddItem::updatePartners()
{
    QStringList items=sp_getCategory("partner",table);
    if(!items.isEmpty())
        ui->partners->addItems(items);

    ui->partners->addItem(MANUAL_INPUT);
    ui->partners->setCurrentIndex(-1);
}

void AF_AddItem::updateNames()
{
    QStringList items=sp_getCategory("name",table);
    if(!items.isEmpty())
        ui->names->addItems(items);

    ui->names->addItem(MANUAL_INPUT);
    ui->names->setCurrentIndex(-1);
}

void AF_AddItem::setItem(const QString &name,
                         const QString &number,
                         const QString &remark)
{
    ui->isMulti->setEnabled(false);

    ui->names->setCurrentText(name);
    ui->names->setEnabled(false);

    ui->number->setText(number);
    ui->remark->setText(remark);
}

void AF_AddItem::on_isMulti_stateChanged(int state)
{
    if(state == Qt::Checked)
    {
        ui->types->setEnabled(false);
        ui->type->setEnabled(false);
        ui->partners->setEnabled(false);
        ui->partner->setEnabled(false);
    }
    else
    {
        ui->types->setEnabled(true);
        ui->type->setEnabled(true);
        ui->partners->setEnabled(true);
        ui->partner->setEnabled(true);
    }
}

bool AF_AddItem::addItem(const QString &type,
                         const QString &partner,
                         const QString &name,
                         int number,
                         const QString &remark)
{
    QSqlQuery query;
    query.prepare(QString("insert into %1 (month_,date_,type_,partner,name,number,remark) "
                          "values (?,?,?,?,?,?,?)")
                  .arg(table));
    query.addBindValue(ui->dateEdit->date().toString(MONTH_FORMAT));
    query.addBindValue(ui->dateEdit->date().toString(DATE_FORMAT));
    query.addBindValue(type);
    query.addBindValue(partner);
    query.addBindValue(name);
    query.addBindValue(number);
    query.addBindValue(remark);
    if(query.exec())
        return true;
    else
    {
        qDebug() << query.lastError().text();
        return false;
    }
}

void AF_AddItem::on_btn_submit_clicked()
{
    QString type_=ui->type->text().trimmed();
    QString partner=ui->partner->text().trimmed();
    if(type_.isEmpty() || partner.isEmpty())
        return;

    QString name=ui->name->text().trimmed();
    if(name.isEmpty())
    {
        MESSAGE_LABEL_EMPTY
    }

    QString numberStr=ui->number->text().trimmed();
    if(numberStr.isEmpty())
        return;

    bool ok;
    int number=numberStr.toInt(&ok);
    if(!ok)
    {
        MESSAGE_INPUT_ERROR
    }

    QString remark=ui->remark->text().trimmed();
    if(addItem(type_,partner,name,number,remark))
    {
        if(ui->isMulti->isChecked())
        {
            ui->names->setCurrentIndex(-1);
            ui->number->clear();
            ui->remark->clear();
            emit accepted();    // 只是发出信号
        }
        else
            accept();       //不仅发出信号，还会关闭窗体
    }
    else
        MESSAGE_CANNOT_SUBMIT
}

void AF_AddItem::on_types_currentTextChanged(const QString &text)
{
    if(text == MANUAL_INPUT)
    {
        ui->type->clear();
        ui->type->setEnabled(true);
    }
    else
    {
        ui->type->setText(text);
        ui->type->setEnabled(false);
    }
}

void AF_AddItem::on_partners_currentTextChanged(const QString &text)
{
    if(text == MANUAL_INPUT)
    {
        ui->partner->clear();
        ui->partner->setEnabled(true);
    }
    else
    {
        ui->partner->setText(text);
        ui->partner->setEnabled(false);
    }
}

void AF_AddItem::on_names_currentTextChanged(const QString &text)
{
    if(text == MANUAL_INPUT)
    {
        ui->name->clear();
        ui->name->setEnabled(true);
    }
    else
    {
        ui->name->setText(text);
        ui->name->setEnabled(false);
    }
}
